// index.ts
import createRouter from "../../router/routers"
import { store } from "../../store/store"
import { isLogin } from "../../utils/isLogin"
import Api from '../api/index'
const api = new Api()

Page({
	data: {
		isAdvertising: false,
		pics: [{ id: '001', url: '/resources/banner/1.png' }, { id: '002', url: '/resources/banner/2.png' }, { id: '003', url: '/resources/banner/3.png' }],
		menus: [{
			title: '美味早餐',
			url: '/resources/menu/breakfast.png'
		}, {
			title: '精致午餐',
			url: '/resources/menu/lunch.png'
		}, {
			title: '饮品',
			url: '/resources/menu/drink.png'
		}, {
			title: '点心',
			url: '/resources/menu/snake.png'
		}],
		goods: [{
			id: '001',
			pic: "/resources/goods/1.png",
			title: '牛肉面',
			num: '3',
			price: '20'
		}, {
			id: '002',
			pic: "/resources/goods/1.png",
			title: 'sdfas sadfas sdalkfjslsdfasdflkj ',
			num: '3',
			price: '20'
		}, {
			id: '003',
			pic: "/resources/goods/1.png",
			title: '牛肉面啥打法是否阿斯顿发生大发',
			num: '3',
			price: '20'
		}],
		advLoc: 0,  //悬浮动画距离右边位置
		scrollHeight: '',
		bottomLift: getApp().globalData.bottomLift, // 底部tabbar距离
		tabBarHeight: getApp().globalData.tabBarHeight,
		isMove: false,
		swiperIndex: 0,
	},
	onLoad() {
		let that = this
		if (!wx.getStorageSync('advertisement')) {
			that.setData({
				isAdvertising: true
			})
			wx.hideTabBar({});// 隐藏底部按钮
		}

		// 获取openid
		let openid = wx.getStorageSync('openid')
		if (!openid) {
			this.getOpenId()
		}

		// this.getData()
	},
	onShow() {
		// 自定义tabbar
		if (typeof this.getTabBar === 'function' &&
			this.getTabBar()) {
			this.getTabBar().setData({
				selected: 0
			})
		}
		this.adaptation()
	},

	// 去搜索页
	gotoSearch() {
		createRouter().push('/home/search')
	},

	getOpenId() {
		// 微信登陆
		wx.login({
			success(res) {
				if (res.code) {
					console.log(res.code)
					api.wxLogin({ c: res.code }).then((res) => {
						if (res) {
							wx.setStorageSync('openid', res)
						}
					})
				} else {
					console.log('登录失败！' + res.errMsg)
				}
			}
		})
	},

	//云开发
	//数据库中获取数据信息
	getData() {
		let that = this;
		//获取数据库对象
		const db = wx.cloud.database();
		//获取
		db.collection('goods')
			// .where({
			//     //条件查询
			//     name: '热干面'
			// })
			.get()
			.then((res) => {
				//请求成功
				let g: any[] = []
				res.data.map((item: any) => {
					g.push({
						id: item._id,
						pic: "/resources/goods/1.png",
						title: item.name,
						num: '3',
						price: item.price
					})
				})
				that.setData({
					goods: g
				})
			}).catch((err) => {
				//请求失败
				console.log(err)
			})
	},

	// 适配高度
	adaptation() {
		const that = this;
		setTimeout(function () {
			var query = wx.createSelectorQuery();
			query.select('.topTitle').boundingClientRect();
			query.select('.topSearch').boundingClientRect();
			query.exec(function (rect) {
				if (rect[0] === null) return;
				let num = 0;
				rect.forEach(element => {
					if (element != null) {
						num += element.height
					}
				});

				that.setData({
					// 标题 + 搜索框 + 滑动栏 + 底部栏  + 适配距离
					scrollHeight: `calc(100vh - ${rect[0].height + rect[1].height + that.data.bottomLift}px - ${that.data.tabBarHeight}rpx)`, //左侧栏
				})
			});
		}, 500)
	},
	//监听页面滚动
	scroll(e: any) {
		let scrollTop = e.detail.scrollTop;
		// 动画
		let animation = wx.createAnimation({
			duration: 1000,
			timingFunction: 'ease'
		});
		if (scrollTop > 15) {
			animation.translateX(30).step()
			this.setData({
				ani: animation.export()
			})
		} else {
			animation.translateX(0).step()
			this.setData({
				ani: animation.export()
			})
		}
	},
	// 关闭开屏广告
	onClose() {
		wx.setStorageSync("advertisement", true)
		this.setData({
			isAdvertising: false
		})
		wx.showTabBar({
			animation: true//动画效果
		});
	},
	// 点击开屏广告
	clickAd() {
		createRouter().push("/home/bannerDetail")
	},
	toBannerDetail(event: any) {
		/**
		 * event.currentTarget.dataset.id：获取参数
		 */
		let id: string = event.currentTarget.dataset.id
		createRouter().push("/home/bannerDetail", { id })

	},
	toTypePage() {
		if (!isLogin) {
			createRouter().reLaunch("/login")
			return;
		}
		wx.redirectTo({
			url: '/pages/mall/mall'
		})
	},
	toGoodDetail(event: any) {
		console.log(event.currentTarget.dataset.id)
		let id: String = event.currentTarget.dataset.id
		createRouter().push("/home/goodDetail", { id })
	},

	//轮播图切换
	swiperChange(e) {
		//轮播图当前的index：e.detail.current
		this.setData({
			swiperIndex: e.detail.current
		})
	}
})
